package com.admin.controller.admin.monitor;

import com.system.constant.HTTPStatus;
import com.system.response.Result;
import com.system.response.monitor.logs.OperLogDtoResponse;
import com.system.service.monitor.logs.SysOperLogService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Classname OperLog
 * @Date 2023/8/1 16:35
 * @Created ZFC
 */
@RestController
@RequestMapping("/sys/log/operlog")
@Log4j
public class SysOperLogController {

    @Autowired
    private SysOperLogService operLogService;

    @Cacheable(
            value = "operlog",
            key = "#operLogDto.pageSize +'::'+ #operLogDto.pageNum +'::'+ " +
                    "#operLogDto.title +'::'+ #operLogDto.operName +'::'+ " +
                    "#operLogDto.businessType +'::'+ #operLogDto.status +'::'+ " +
                    "#operLogDto.startTime +'::'+ #operLogDto.endTime"
    )
    @ApiOperation("条件查询操作日志")
    @GetMapping("/list")
    public Result list(OperLogDtoResponse operLogDto) {
        return operLogService.selectOperLogList(operLogDto);
    }

    @RequiresPermissions("admin:operlog:delete")
    @DeleteMapping("/deleteOperLog")
    public Result deleteOperLogByIds(@NotNull(message = "参数为空") Long[] ids) {
        return operLogService.deleteOperLogByIds(ids);
    }

    @RequiresPermissions("admin:operlog:clear")
    @DeleteMapping("/clear")
    public Result clearOperLog() {
        return operLogService.cleanOperLog();
    }

    @CacheEvict(
            value = "operlog",
            allEntries = true
    )
    @DeleteMapping("/clearCache")
    public Result clearCache() {
        return new Result().setCode(HTTPStatus.SUCCESS).setMessage("清空成功！");
    }

}
